package com.joven.shelltest.utils.sharefactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wzj
 * @Date: 2024/4/8 11:30
 */
public class CommodityFactory {

    /**
     * 享元设计模式是工厂模式的扩展，多了一步缓存对象处理
     */
    public Map<String, Commodity> commodityMap = new HashMap<>();

    public Commodity getCommodityInstance(String key) {
        if (!commodityMap.containsKey(key)) {
            if ("拖鞋".equals(key)) {
                commodityMap.put(key,new Slipper("超级拖鞋（航天品质）"));
            } else if ("手机".equals(key)) {
                commodityMap.put(key,new Phone("超级手机（聚变发电）"));
            } else {
                throw new RuntimeException("不存在的商品型号");
            }
        }
        return commodityMap.get(key);
    }
}
